package com.xiaoyu;

import com.xiaoyu.model.Account;
import com.xiaoyu.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@Api(tags = "账户认证及账户管理接口")
@RestController
//@CrossOrigin(origins = "http://39.101.65.116:8080")
@CrossOrigin(origins = "http://localhost:8080")
public class AccountController {
    @Autowired
    private AccountService accountService;


    @ApiOperation("登录接口")
    @PostMapping("/login")
    public Account login(@RequestBody Account account , HttpSession session){
        Account a= accountService.login(account);
        if(a!=null&&a.getState()==1){
            session.setAttribute("loginAccount",a);
            return a;
        }
        return null;
    }

    @GetMapping("/quit")
    public void quit(HttpSession session){
        session.removeAttribute("loginAccount");
    }

    @ApiOperation("权限获取")
    @GetMapping("/loginPost")
    public String loginPost(HttpSession session){
        Account a= (Account)session.getAttribute("loginAccount");
        if (a!=null){
            return a.getPostList().get(0).getPname();
        }
        return null;
    }

    @ApiOperation("账户列表接口")
    @GetMapping("/accountList")
    public List<Account> accountList(){
        return accountService.allAccount();
    }

    @ApiOperation("用户密码获取接口")
    @GetMapping("/getPass")
    public String getPass(HttpSession session){
        Account a= (Account)session.getAttribute("loginAccount");
        if (a!=null){
            return a.getPassword();
        }
        return null;
    }

    @ApiOperation("修改密码接口")
    @PostMapping("/password")
    public void password(@RequestBody Account account){
        if(account.getPassword()!=null&&account.getId()!=null&&!account.getPassword().equals("")){
            accountService.updateAccount(account);
        }
    }

    @ApiOperation("修改状态接口")
    @PostMapping("/state")
    public String state(@RequestBody Account account,HttpSession session){
        Account a= (Account)session.getAttribute("loginAccount");
        if(a.getId()==account.getId())
            return "self";
        else if(account.getState()!=null&&account.getId()!=null){
            accountService.updateAccount(account);
            return "success";
        }
        return "error";
    }

    @ApiOperation("修改账户接口")
    @PostMapping("/account/{post}")
    public String updateAccount(@PathVariable String post,
                                @RequestBody Account account,HttpSession session){
        Account a= (Account)session.getAttribute("loginAccount");
        if(account.getId()==1)
            return "root";
        else if(a.getId()==account.getId())
            return "self";
        else if(account.getPassword()!=null&&account.getId()!=null&&!account.getAccountName().equals("")){
            accountService.updateAccountClient(account.getId(),post);
            accountService.updateAccount(account);
            return "success";
        }
        return "error";
    }

    @GetMapping("/null")
    public String noLogin(){
        return null;
    }
}
